package com.koron.order.defectDemotion.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.order.common.tools.CommonCodeTools;
import com.koron.order.common.tools.OrderCodeEnum;
import com.koron.order.defect.bean.dto.DefectDTO;
import com.koron.order.defect.bean.po.DefectBean;
import com.koron.order.defect.bean.vo.DispatchVO;
import com.koron.order.defect.service.DefectService;
import com.koron.order.defect.service.DispatchService;
import com.koron.order.defectDemotion.bean.po.DefectDemotionPo;
import com.koron.order.defectDemotion.bean.po.DefectDemotionStatus;
import com.koron.order.defectDemotion.bean.query.DefectDemotionQuery;
import com.koron.order.defectDemotion.bean.vo.DefectDemotionVo;
import com.koron.order.defectDemotion.mapper.DefectDemotionMapper;
import com.koron.order.defectDemotion.service.DefectDemotionService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 缺陷工单下缺陷降级信息从表(DefectDemotion)表服务实现类
 *
 * @author zhouj
 * @since 2022-02-09 10:36:50
 */
@Service("defectDemotionService")
public class DefectDemotionServiceImpl implements DefectDemotionService {

    @Autowired
    DefectService defectService;

    @Autowired
    private WorkFlowHandler workFlowHandler;

    @Autowired
    private DispatchService dispatchService;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public DefectDemotionPo insertOrUpdate(SessionFactory factory, DefectDemotionPo defectDemotion) {

        DefectDemotionMapper mapper = factory.getMapper(DefectDemotionMapper.class);

        if (StringUtils.isEmpty(defectDemotion.getId())) {
            DefectBean defectBean = defectService.queryById(factory, defectDemotion.getDefectId());
            defectDemotion.setCode(CommonCodeTools.getNormalOrderCode(defectBean.getCode(), OrderCodeEnum.JJ, 2));
            defectDemotion.setStatus(DefectDemotionStatus.DRAFT.getStatus());
        }

        mapper.insertOrUpdate(defectDemotion);

        return defectDemotion;
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<DefectDemotionPo> defectDemotionList) {

        DefectDemotionMapper mapper = factory.getMapper(DefectDemotionMapper.class);

        return mapper.insertOrUpdateBatch(defectDemotionList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, DefectDemotionPo defectDemotion) {

        DefectDemotionMapper mapper = factory.getMapper(DefectDemotionMapper.class);

        return mapper.insert(defectDemotion);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<DefectDemotionPo> defectDemotionList) {

        DefectDemotionMapper mapper = factory.getMapper(DefectDemotionMapper.class);

        return mapper.insertBatch(defectDemotionList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, DefectDemotionPo defectDemotion) {
        DefectDemotionMapper mapper = factory.getMapper(DefectDemotionMapper.class);
        return mapper.update(defectDemotion);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        DefectDemotionMapper mapper = factory.getMapper(DefectDemotionMapper.class);
        int i = mapper.deleteById(id);
        workFlowHandler.delete(id);
        return i;
    }

    @Override
    @TaskAnnotation("queryById")
    public DefectDemotionPo queryById(SessionFactory factory, String id) {

        DefectDemotionMapper mapper = factory.getMapper(DefectDemotionMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<DefectDemotionPo> queryList(SessionFactory factory, DefectDemotionPo defectDemotion) {

        DefectDemotionMapper mapper = factory.getMapper(DefectDemotionMapper.class);

        List<DefectDemotionPo> list = mapper.queryList(defectDemotion);

        return list;
    }

    @Override
    @TaskAnnotation("queryVoPageList")
    public PageInfo queryVoPageList(SessionFactory factory, DefectDemotionQuery demotionQuery, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy(false));
        DefectDemotionMapper mapper = factory.getMapper(DefectDemotionMapper.class);

        List<DefectDemotionVo> list = mapper.queryVoList(demotionQuery);

        return new PageInfo(list);
    }

    /*
     * 功能描述 提交工单状态接口
     * @author zhouj
     * @date 2021/12/21 10:55
     * @param [factory, orderId:工单id, status:将要改变的状态]
     * description 对于状态的修改需要进行顺序的判断,只能往后流转,
     * @return int
     */
    @Override
    @TaskAnnotation("submitDefectDemotion")
    public int submitDefectDemotion(SessionFactory factory, String orderId, String status) {

        DefectDemotionMapper mapper = factory.getMapper(DefectDemotionMapper.class);
        //根据工单id 先判断工单当时的状态
        DefectDemotionPo preDefectDemotionPo = mapper.queryById(orderId);
        String preStatus = preDefectDemotionPo.getStatus();

        //将要改变的状态
        DefectDemotionStatus defectDemotionStatus = DefectDemotionStatus.getDefectDemotionStatus(status);
        Assert.notNull(defectDemotionStatus, "缺陷降级状态修改,枚举不能为空");

        if (StringUtils.isNotEmpty(preStatus)) {
            DefectDemotionStatus preDefectDemotionStatus = DefectDemotionStatus.getDefectDemotionStatus(preStatus);
            Assert.notNull(preDefectDemotionStatus, "缺陷降级数据有误");
            Assert.isTrue(defectDemotionStatus.getSort() - preDefectDemotionStatus.getSort() == 1, "不可重复提交");
        } else {
            Assert.isTrue(StringUtils.equalsAny(defectDemotionStatus.getStatus(),
                    DefectDemotionStatus.DRAFT.getStatus(),
                    DefectDemotionStatus.WAIT_REVIEW.getStatus()), "修改缺陷降级工单状态失败");
        }
        DefectDemotionPo po = new DefectDemotionPo();
        po.setId(orderId);
        po.setStatus(status);

        // 如果是审核完成,即改为已审批时,需要更改缺陷工单的预计完成时间,和缺陷工单的缺陷等级
        // 同时如果有派工信息，则需要修改计划完成时间
        //缺陷工单id
        if (StringUtils.equalsIgnoreCase(status, DefectDemotionStatus.REVIEWED.getStatus())) {
            DefectDTO defectDTO = new DefectDTO();
            defectDTO.setId(preDefectDemotionPo.getDefectId());
            defectDTO.setLevel(preDefectDemotionPo.getNewLevel());
            DefectBean defectBean = defectService.saveOrUpdate(factory, defectDTO);
            po.setFinishTime(new Date());

            DispatchVO dispatchVOQuery = new DispatchVO();
            dispatchVOQuery.setOrderId(defectDTO.getId());
            DispatchVO dispatchVO = dispatchService.querySingle(factory, dispatchVOQuery);
            if (!ObjectUtils.isEmpty(dispatchVO)){
                dispatchVO.setEndTime(defectBean.getExpectEndTime());
                dispatchService.saveDispatch(factory,dispatchVO);
            }
        }

        return mapper.update(po);
    }

}